package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.UserBank;
import com.gaogzhen.dto.UserBankDto;
import com.gaogzhen.feigh.UserBankServiceFeign;
import com.gaogzhen.service.UserBankService;
import com.gaogzhen.utils.CurrentUser;
import com.gaogzhen.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Administrator
 * @version 1.0
 * @description 会员银行卡管理器
 * @date 2022-06-08 11:28
 */
@RestController
@RequestMapping("/userBanks")
@Api(tags = "会员银行卡管理器")
public class UserBankController implements UserBankServiceFeign {

    @Autowired
    UserBankService userBankService;

    @GetMapping
    @ApiOperation("分页查询会员银行卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "userId", value = "会员id")
    })
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> findByPage(@ApiIgnore Page<UserBank> page, Long userId) {
        return R.ok(userBankService.findByPage(page, userId));

    }

    @PatchMapping("/status")
    @ApiOperation("启用禁用银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "银行卡id"),
            @ApiImplicitParam(name = "status", value = "状态")
    })
    @PreAuthorize("hasAuthority('user_bank_update')")
    public R updateStatus(Long id, Integer status) {
        userBankService.updateStatus(id, status);
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("修改银行卡信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userBank", value = "银行卡信息")
    })
    @PreAuthorize("hasAuthority('user_bank_update')")
    public R updateStatus(@RequestBody @Validated UserBank userBank) {
        userBankService.updateUserBank(userBank);
        return R.ok();
    }

    @GetMapping("/current")
    @ApiOperation("查询当前用户的银行卡信息")
    public R<UserBank> getCurrentUserBank() {
        return R.ok(userBankService.getCurrentUserBank(CurrentUser.getCurrentUserId()));
    }

    @PostMapping("/bind")
    @ApiOperation("修改或者添加用户的银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userBank", value = "银行卡信息")
    })
    public R bindBank(@RequestBody @Validated UserBank userBank) {
        userBankService.bindBank(CurrentUser.getCurrentUserId(), userBank);
        return R.ok();
    }


    @Override
    public UserBankDto getBriefUserBank(Long userId) {
        return userBankService.getBriefUserBank(userId);
    }
}
